<template>
	<view class="page">
		<view class="pages-1">
			<image :src="inviteImgUrl" mode="widthFix"  class="inviteImgUrl"></image>
			<!-- <view class="box">
				<view class="box1">
					<image :src="h5Url" mode="" class="h5Url"></image>
				</view>
			</view> -->
			<view class="btn primary mt-40 save-img-box flex-row flex-center radius-10" @click="saveBase64Img">
				长按图片保存
			</view>
		</view>
		
	</view>
</template>

<script>
	import Qrcode from 'qrious';
	export default {
		data() {
			return {
				h5Url:null,
				posterUrl:null,
				inviteImgUrl:null,
			}
		},
		onLoad(options){
			this.posterUrl = uni.getStorageSync('posterUrl');
			this.inviteImgUrl = "data:image/png;base64,"+uni.getStorageSync('posterUrl');
		},
		methods: {
			saveBase64Img() {
			// #ifdef APP-PLUS
			      let imageStr = this.inviteImgUrl;
			      // 保存到本地
			      let bitmap = new plus.nativeObj.Bitmap("goods_poster");
			      bitmap.loadBase64Data(
			        imageStr,
			        () => {
			          console.log("加载Base64图片数据成功");
			          bitmap.save(
			            "_doc/goods_poster.png",
			            {},
			            (i) => {
			              console.log("保存图片成功：" + JSON.stringify(i));
			              bitmap.clear();
			            },
			            (e) => {
			              onsole.log("保存图片失败：" + JSON.stringify(e));
			              bitmap.clear();
			            }
			          );
			        },
			        (e) => {
			          console.log("加载Base64图片数据失败：" + JSON.stringify(e));
			          bitmap.clear();
			        }
			      );
			// #endif
			// #ifdef MP
			      var fileManager = uni.getFileSystemManager();
			      fileManager.writeFile({
			        filePath: "/img.jpg", // 指定图片的临时路径
			        data: this.posterUrl, // 要写入的文本或二进制数据
			        encoding: "base64", // 指定写入文件的字符编码
			        success: (res) => {
			          console.log("写入文件成功，文件路径信息为：", res);
			          savePosterPath(res)
			        },
			        file: (err) => {
			          console.log("写入文件失败", err);
			        },
			      });
			// #endif
			},
			// 小程序保存图片
			    savePosterPath(url) {
			      uni.downloadFile({
			        url,
			        success: (resFile) => {
			          console.log(resFile, "resFile");
			          if (resFile.statusCode === 200) {
			            uni.getSetting({
			              success: (res) => {
			                if (!res.authSetting["scope.writePhotosAlbum"]) {
			                  uni.authorize({
			                    scope: "scope.writePhotosAlbum",
			                    success: () => {
			                      uni.saveImageToPhotosAlbum({
			                        filePath: resFile.tempFilePath,
			                        success: (res) => {
			                          return uni.showToast({
			                            title: "保存成功！",
			                          });
			                        },
			                        fail: (res) => {
			                          return uni.showToast({
			                            title: res.errMsg,
			                          });
			                        },
			                        complete: (res) => {},
			                      });
			                    },
			                    fail: () => {
			                      uni.showModal({
			                        title: "您已拒绝获取相册权限",
			                        content: "是否进入权限管理，调整授权？",
			                        success: (res) => {
			                          if (res.confirm) {
			                            uni.openSetting({
			                              success: (res) => {
			                                console.log(res.authSetting);
			                              },
			                            });
			                          } else if (res.cancel) {
			                            return uni.showToast({
			                              title: "已取消！",
			                            });
			                          }
			                        },
			                      });
			                    },
			                  });
			                } else {
			                  uni.saveImageToPhotosAlbum({
			                    filePath: resFile.tempFilePath,
			                    success: (res) => {
			                      return uni.showToast({
			                        title: "保存成功！",
			                      });
			                    },
			                    fail: (res) => {
			                      return uni.showToast({
			                        title: res.errMsg,
			                      });
			                    },
			                    complete: (res) => {},
			                  });
			                }
			              },
			              fail: (res) => {},
			            });
			          } else {
			            return uni.showToast({
			              title: resFile.errMsg,
			            });
			          }
			        },
			        fail: (res) => {
			          return uni.showToast({
			            title: res.errMsg,
			          });
			        },
			      });
			    },
				// APP保存图片
				    savePhoto(url) {
				      // 先下载图片
				      uni.downloadFile({
				        url,
				        success: (res) => {
				          // 获取到图片本地地址后再保存图片到相册（因为此方法不支持远程地址）
				          uni.saveImageToPhotosAlbum({
				            filePath: res.tempFilePath,
				            success: () => {
				              uni.showToast({
				                title: "保存成功！",
				              });
				            },
				            fail: () => {
				              uni.showToast({
				                title: "保存失败",
				              });
				            },
				          });
				        },
				      });
				    }
		}
	}
</script>

<style lang="scss" scoped>
	.pages-1{
		width: 100vw;
		height: 100vh;
		.inviteImgUrl{
			position: relative;
			width: 100vw;
			height: 100vh;
		}
		.box{
			width: 350rpx;
			height: 350rpx;
			left: 0;
			position: absolute; 
			left: 50%;
			top: 50%;
			border:1rpx solid #eee;
			background-color: white;
			transform: translate(-50%, -50%); 
			border-radius: 5%;
				.box1{
					width: 350rpx;
					height: 350rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					.h5Url{
						width: 300rpx;
						height: 300rpx;
					}
				}
		}
	
	}
	.save-img-box{
			height: 88rpx;
			margin:0 60rpx;
		}
</style>